package cn.iocoder.yudao.module.biz.controller.admin.zmorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.zmorder.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.zmorder.ZmOrderDO;
import cn.iocoder.yudao.module.biz.service.zmorder.ZmOrderService;

@Tag(name = "管理后台 - 订单报")
@RestController
@RequestMapping("/biz/zm-order")
@Validated
public class ZmOrderController {

    @Resource
    private ZmOrderService zmOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建订单报")
    @PreAuthorize("@ss.hasPermission('biz:zm-order:create')")
    public CommonResult<Long> createZmOrder(@Valid @RequestBody ZmOrderSaveReqVO createReqVO) {
        return success(zmOrderService.createZmOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单报")
    @PreAuthorize("@ss.hasPermission('biz:zm-order:update')")
    public CommonResult<Boolean> updateZmOrder(@Valid @RequestBody ZmOrderSaveReqVO updateReqVO) {
        zmOrderService.updateZmOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:zm-order:delete')")
    public CommonResult<Boolean> deleteZmOrder(@RequestParam("id") Long id) {
        zmOrderService.deleteZmOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:zm-order:query')")
    public CommonResult<ZmOrderRespVO> getZmOrder(@RequestParam("id") Long id) {
        ZmOrderDO zmOrder = zmOrderService.getZmOrder(id);
        return success(BeanUtils.toBean(zmOrder, ZmOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单报分页")
    @PreAuthorize("@ss.hasPermission('biz:zm-order:query')")
    public CommonResult<PageResult<ZmOrderRespVO>> getZmOrderPage(@Valid ZmOrderPageReqVO pageReqVO) {
        PageResult<ZmOrderDO> pageResult = zmOrderService.getZmOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZmOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单报 Excel")
    @PreAuthorize("@ss.hasPermission('biz:zm-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZmOrderExcel(@Valid ZmOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZmOrderDO> list = zmOrderService.getZmOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单报.xls", "数据", ZmOrderRespVO.class,
                        BeanUtils.toBean(list, ZmOrderRespVO.class));
    }

}